package utils;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

public class BitmapLoader extends AsyncTask<String, Void, Bitmap>{
	
	Bitmap bitmap;
	ImageView imageView;
	InputStream in;
	BufferedOutputStream buffOut;
	
	public BitmapLoader(ImageView imView) {
		imageView = imView;
		bitmap = null;
	}
	

	@Override
	protected Bitmap doInBackground(String... urls) {
		String url = urls[0];
		try {
			in = (InputStream) new URL(url).getContent();
			bitmap = BitmapFactory.decodeStream(in);
			Log.d("BitmapLoader", "loading bitmap...");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e)  {
			e.printStackTrace();
		}
		return bitmap;
	}
	
	@Override
	protected void onPostExecute(Bitmap result) {
		imageView.setImageBitmap(result);
	}
}
